package cc.rengu.igas.mcps.common.dao.impl;

import cc.rengu.igas.mcps.common.dao.TermKeyMapper;
import cc.rengu.igas.mcps.common.entity.TermKey;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

public class TermKeyMapperImpl implements TermKeyMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);

    @Override
    public TermKey getTermKey(String instId, String mchntNo, String termNo, String keyType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMcpsTermKeyByPk");
        TermKey termKey = new TermKey();
        termKey.setInstId(instId);
        termKey.setMchntNo(mchntNo);
        termKey.setTermNo(termNo);
        termKey.setKeyType(keyType);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, termKey, termKey);
        if (0 != iReturnCode) {
            rglog.error("获取终端密钥信息失败,mchntNo:<{}>termNo:<{}>keyType:<{}>", mchntNo, termNo, keyType);
            return null;
        }
        return termKey;
    }

    @Override
    public int insertTermKey(TermKey termKey) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertOnce");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, termKey, termKey);
        if (0 != iReturnCode) {
            rglog.error("插入终端密钥信息失败,mchntNo:<{}>termNo:<{}>keyType:<{}>", termKey.getMchntNo(), termKey.getTermNo(), termKey.getKeyType());
        }
        return iReturnCode;
    }

    @Override
    public int insertTermKeyTransaction(TermKey termKey) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertOnce");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, termKey, termKey);
        if (0 != iReturnCode) {
            rglog.error("获取终端密钥信息失败,mchntNo:<{}>termNo:<{}>keyType:<{}>", termKey.getMchntNo(), termKey.getTermNo(), termKey.getKeyType());
        }
        return iReturnCode;
    }

    @Override
    public int updateTermKeyByPk(TermKey termKey) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMcpsTermKeyByPk");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, termKey, termKey);
        if (0 != iReturnCode) {
            rglog.error("更新终端密钥信息失败,mchntNo:<{}>termNo:<{}>keyType:<{}>", termKey.getMchntNo(), termKey.getTermNo(), termKey.getKeyType());
        }
        return iReturnCode;
    }

    @Override
    public int updateTermKeyByPkTransaction(TermKey termKey) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMcpsTermKeyByPk");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, termKey, termKey);
        if (0 != iReturnCode) {
            rglog.error("更新终端密钥信息失败,mchntNo:<{}>termNo:<{}>keyType:<{}>", termKey.getMchntNo(), termKey.getTermNo(), termKey.getKeyType());
        }
        return iReturnCode;
    }
}
